package com.gimi.cloud.jimi.merchantZone.controller.store;

import com.gimi.cloud.jimi.common.enums.ResponseCodeEnum;
import com.gimi.cloud.jimi.common.utils.JsonUtil;
import com.gimi.cloud.jimi.common.utils.UUIDUtil;
import com.gimi.cloud.jimi.common.utils.oss.AliOSSUtil;
import com.gimi.cloud.jimi.common.utils.reqhead.Response;
import com.gimi.cloud.jimi.common.utils.reqhead.Util;
import com.gimi.cloud.jimi.dto.store.MerchantRangeDTO;
import com.gimi.cloud.jimi.dto.store.StoreDTO;
import com.gimi.cloud.jimi.merchantZone.service.store.StoreService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.MultiValueMap;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.support.StandardMultipartHttpServletRequest;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.InputStream;
import java.util.*;

/**
 * Created by shazhiwei on 2017/12/20.
 */
@RestController
@RequestMapping(value = "store")
public class StoreController {
    private final StoreService storeService;
    private static Logger logger = LoggerFactory.getLogger(StoreController.class);

    @Autowired
    public StoreController(StoreService storeService){
        this.storeService = storeService;
    }

    @RequestMapping(value = "/add")
    public void addStore(@RequestBody StoreDTO params, HttpServletResponse response) {
        Response resp = new Response();
        if(storeService.addStore(params)) {
            resp.setResponseCode(ResponseCodeEnum.SUCCESSED);
        }else{
            resp.setResponseCode(ResponseCodeEnum.STORE_ADD_ERR);
        }
        Util.sendResponse(JsonUtil.toJson(resp), response);
    }

    @RequestMapping(value = "/list")
    public void storeList(@RequestBody StoreDTO dto, HttpServletResponse response) {
        Response resp = new Response();
        resp.setResponseCode(ResponseCodeEnum.SUCCESSED);
        resp.setRes_data(storeService.getStoreListByCondition(dto));
        Util.sendResponse(JsonUtil.toJSONStringWithDateFormatYYYYMMDDHHMMSS(resp), response);
    }

    @RequestMapping(value = "/allList")
    public void allStoreList(@RequestBody StoreDTO dto, HttpServletResponse response) {
        Response resp = new Response();
        resp.setResponseCode(ResponseCodeEnum.SUCCESSED);
        resp.setRes_data(storeService.allStoreList(dto));
        Util.sendResponse(JsonUtil.toJSONStringWithDateFormatYYYYMMDDHHMMSS(resp), response);
    }

    @RequestMapping(value = "/single")
        public void storeSingle(@RequestBody StoreDTO dto, HttpServletResponse response) {
            Response resp = new Response();
            resp.setResponseCode(ResponseCodeEnum.SUCCESSED);
            resp.setRes_data(storeService.getStoreSingleByCondition(dto));
            Util.sendResponse(JsonUtil.toJson(resp), response);
    }

    @RequestMapping(value = "/auth")
    public void storeAuth(@RequestBody StoreDTO dto, HttpServletResponse response) {
        Response resp = new Response();
        resp.setResponseCode(ResponseCodeEnum.SUCCESSED);
        resp.setRes_data(storeService.storeAuth(dto));
        Util.sendResponse(JsonUtil.toJson(resp), response);
    }

    @RequestMapping(value = "/update")
    public void updateStore(@RequestBody StoreDTO dto, HttpServletResponse response) {
        Response resp = new Response();
        if(storeService.updateStore(dto)) {
            resp.setResponseCode(ResponseCodeEnum.SUCCESSED);
        }else{
            resp.setResponseCode(ResponseCodeEnum.STORE_UPDATE_ERR);
        }
        Util.sendResponse(JsonUtil.toJson(resp), response);
    }

    @RequestMapping(value = "/delete")
    public void storeDelete(@RequestBody StoreDTO dto, HttpServletResponse response) {
        Response resp = new Response();
        if(storeService.deleteStore(dto)) {
            resp.setResponseCode(ResponseCodeEnum.SUCCESSED);
        }else{
            resp.setResponseCode(ResponseCodeEnum.STORE_DELETE_ERR);
        }
        Util.sendResponse(JsonUtil.toJson(resp), response);
    }

    @RequestMapping(value = "/fileUpload")
    public void fileUpload(HttpServletRequest request, HttpServletResponse response) throws Exception {
        String url = "";
        StandardMultipartHttpServletRequest multipartHttpServletRequest = (StandardMultipartHttpServletRequest) request;
        MultiValueMap multipartFiles = multipartHttpServletRequest.getMultiFileMap();
        for(Object entry : multipartFiles.entrySet()){
            LinkedList<MultipartFile> lists = ((Map.Entry<String, LinkedList<MultipartFile>>) entry).getValue();
            for(MultipartFile file : lists){
                InputStream is = file.getInputStream();
                String uuId = UUIDUtil.get32UUID();
                String dirPath = "store";
                String[] originalFilenames = file.getOriginalFilename().split("\\.");
                String fileName = uuId + "." + originalFilenames[originalFilenames.length-1];
                String result = AliOSSUtil.uploadFile(is,dirPath,fileName);
                String tempUrl = AliOSSUtil.getFileUrl(result,new Date(System.currentTimeMillis() + 1000 * 60 * 60 * 24));
                System.out.println("==========>上传结果：" + url);
                url += ","+tempUrl;
            }
        }

        Response resp = new Response();
        Map<String,String> data = new HashMap<>();
        data.put("url",url.equals("")?url:url.substring(1));
        resp.setRes_data(data);
        resp.setResponseCode(ResponseCodeEnum.SUCCESSED);
        Util.sendResponse(JsonUtil.toJson(resp), response);
    }

    @RequestMapping(value = "/merchant/add")
    public void addMerchantStore(@RequestBody MerchantRangeDTO params, HttpServletResponse response) {
        Response resp = new Response();
        if(storeService.addStoreManchant(params)) {
            resp.setResponseCode(ResponseCodeEnum.SUCCESSED);
        }else{
            resp.setResponseCode(ResponseCodeEnum.STORE_MERCHANT_ADD_ERR);
        }
        Util.sendResponse(JsonUtil.toJson(resp), response);
    }

    @RequestMapping(value = "/merchant/list")
    public void merchantStoreList(@RequestBody MerchantRangeDTO params, HttpServletResponse response) {
        Response resp = new Response();
        resp.setResponseCode(ResponseCodeEnum.SUCCESSED);
        resp.setRes_data(storeService.getMerchantRangeListByCondition(params));
        Util.sendResponse(JsonUtil.toJson(resp), response);
    }

    @RequestMapping(value = "/merchant/category_list")
    public void merchantStoreAddList(@RequestBody MerchantRangeDTO params, HttpServletResponse response) {
        Response resp = new Response();
        resp.setResponseCode(ResponseCodeEnum.SUCCESSED);
        resp.setRes_data(storeService.getProCategoreListByCondition(params));
        Util.sendResponse(JsonUtil.toJson(resp), response);
    }

    @RequestMapping(value = "/merchant/update")
    public void updateMerchantStore(@RequestBody MerchantRangeDTO params, HttpServletResponse response) {
        Response resp = new Response();
        if(storeService.updateStoreManchant(params)) {
            resp.setResponseCode(ResponseCodeEnum.SUCCESSED);
        }else{
            resp.setResponseCode(ResponseCodeEnum.STORE_MERCHANT_UPDATE_ERR);
        }
        Util.sendResponse(JsonUtil.toJson(resp), response);
    }

    @RequestMapping(value = "/merchant/delete")
    public void deleteMerchantStore(@RequestBody MerchantRangeDTO params, HttpServletResponse response) {
        Response resp = new Response();
        if(storeService.deleteMerchantRange(params)) {
            resp.setResponseCode(ResponseCodeEnum.SUCCESSED);
        }else{
            resp.setResponseCode(ResponseCodeEnum.STORE_MERCHANT_DELETE_ERR);
        }
        Util.sendResponse(JsonUtil.toJson(resp), response);
    }
}
